package pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes;

import java.util.Calendar;

import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.utils.TreeUtils;

public class FileNode extends BelowStorageNode {

	private boolean printSuffix = false;
	private long fileSize;
	private Calendar lastModified;
	private String owner;
	
	public String getName() {
		int index = getPath().lastIndexOf("/");
		if (index != -1) {
			String suffix = "";
			if (printSuffix && !getRootNode().areStoragesVisible()) {
				suffix = "(" + getStorage().getName() + ")";
			}
			return getPath().substring(index + 1) + suffix;
		}
		return "No name";
	}
	
	public String getNameWithoutSuffix() {
		int index = getPath().lastIndexOf("/");
		if (index != -1) {
			return getPath().substring(index + 1);
		}
		return "No name";
	}
	
	public void setPrintSuffix(boolean print) {
		printSuffix = print;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}
	
	@Override
	public FileNode clone(Node nodeParent) {
		FileNode fileNode = new FileNode();
		fileNode.setPath(this.getPath());
		fileNode.setPrintSuffix(printSuffix);
		fileNode.setFileSize(fileSize);
		fileNode.setParent(nodeParent);
		
		return fileNode;
	}

	public void setLastModified(Calendar lastModified) {
		this.lastModified = lastModified;
	}
	
	public Calendar getLastModified() {
		return lastModified;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}
	
	public boolean isMy() {
		if ((getStorage().getClient().getSecurityProperties().getCertificateChain().length > 0) && (owner != null)) {
			return owner.equals(TreeUtils.parseUser(getStorage().getClient().getSecurityProperties().getCertificateChain()[0].getSubjectDN().getName()));
		}
		return false;
	}
	
	
}
